<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@ include file="/WEB-INF/taglibs.jsp"%>
<script type="text/javascript" src="${ctx}/resource/system/common/js/ajaxfileupload.js"></script>
<script type="text/javascript">
	$(function(){
		$("#serveMenu").addClass("cur");
	});
</script>

<div class="conTit fixed">
    <p>
   		<c:choose>
			<c:when test="${slideShow.id ==null}">新增轮播图</c:when>
			<c:otherwise>修改轮播图</c:otherwise>
		</c:choose>
    </p>
    <div class="fr"><a href="javascript:void(0);" class="backBtn" onclick="window.history.back(-1);">返回</a></div>
</div>

<div style="margin-left: 10px;">
  <form id="carrierForm" action="" method="post">
	 

	<div class="inputArea">
	     <label>序号:<font style="color: red">*</font></label>
	     <div class="inputLine">
	          <input type="text" id="sort" name="sort" value="${slideShow.sort}" maxlength="2"/>
	     </div>
	</div>
	<div class="inputArea">
	     <label>是否启用:<font style="color: red">*</font></label>
          <input type="radio" name="useStatus" value="1" <c:if test="${slideShow.useStatus == 1}">checked="true"</c:if> />启用
          <input type="radio" name="useStatus" value="0" <c:if test="${slideShow.useStatus == 0}">checked="true"</c:if> />停用
	</div>
<%-- 	<div class="inputArea">
	    <label>链接地址:<font style="color: red">*</font></label>
	     <div class="inputLine">
	          <input type="text" id="linkUrl" name="linkUrl" value="${slideshow.linkUrl}" />
	     </div>
	</div> --%>
	<div class="inputArea02">
	  <label>图片: 建议尺寸：<span>640*320</span> <font style="color: red">*</font></label>
	    <input type="hidden" id="pictureUrl" name="pictureUrl" value="${slideShow.pictureUrl }"/>
	    <div class="inputLine">
	    	   上传轮播图<input id="uploadfile" name="cmfile" type="file" onchange="triggerUpload()" />
	    </div>
	    <div id="imgPreView" >
		        <div id="preview" >
			      	 <div id="thumb" style="margin-left:21%; margin-top:30px;">
			      	 		<c:if test="${slideShow.pictureUrl !=null && slideShow.pictureUrl!='' }">
			      				<img style="width:460px;" src="${imgPath}/${slideShow.pictureUrl}"   />
					    	</c:if>
					 </div>
				</div>
		  </div>
	</div>
 
	<div class="save">
	     <a href="javascript:void(0);" class="backBtn" onclick="saveCarrier()">保存</a>
	</div>
    <input type="hidden" name="id" value="${slideShow.id }" />
  </form>
</div>
		
<script type="text/javascript">

	function saveCarrier() {
		var sort = $("#sort").val();
		var pictureUrl = $("#pictureUrl").val();
		var useStatus = $("input[name=useStatus]:checked").val();
		var success =true;
		if($.trim(useStatus) == ""){
			alert("请选择是否使用");
			success = false;
		}
		if($.trim(sort) ==""){
			alert("请输入序号");
			success = false;
		}	
		else if($.trim(pictureUrl) ==""){
			alert("请上传轮播图");
			success = false;
		}else if(!(/^\d{1,2}$/.test(sort))){ 
			alert("序号格式不正确");
			success = false;
		}
		
 		if(success){
			var params = {
				id : $("input[type=hidden][name=id]").val(),
				sort : sort,
				pictureUrl : pictureUrl,
				useStatus:useStatus
			};
			$.post("${ctx}/admin/slideShow/saveSlideShow.json",
				params,
		        function(result){
		        	result = eval("("+result+")");
		    		if(result.status == "true" || result.status == true){
		    			alert(result.msg);
		    			setTimeout(function(){window.location.href= "${ctx}/admin/slideShow/list.htm";},300);
		    		}else{
		    			alert(result.msg);
		    		}
		        });
		}
	}
	
    function triggerUpload(){
    	  $("#imgPreView").show();
    	  $("#thumb").html('<img src="${ctx}/resource/system/admin/images/loading.gif" width="25" style="display:inline-block;float:left;margin:0 5px 0 0;">'+'<p style="line-height:25px;float:none;color:#78A200;font-size:12px;">文件上传中,请耐心等待...</p>');
		  var fileName = $("#uploadfile").val().toLowerCase();
		  if(fileName.indexOf(".png")>=0 || fileName.indexOf(".jpg")>=0 || fileName.indexOf(".gif")>=0) {
			$.ajaxFileUpload({
				url:'${ctx}/admin/slideShow/uploadSlideShow.json',//用于文件上传的服务器端请求地址
				secureuri:false, //是否需要安全协议，一般设置为false
				fileElementId:'uploadfile',  //文件上传域的ID
				data:{fsize:5},
				dataType: 'json',//返回值类型 一般设置为json
				success: function (data, status) {
					console.log(data);
					$("#thumb").empty();
					if(data.status==true || data.status=="true") {
						$("#pictureUrl").val(data.data.filePath);
						$("#thumb").html('<img style="max-height:320px;max-width: 640px;" src="${imgPath}/'+data.data.filePath+'"/>');
					}else {
						$("#thumb").html(data.msg);
					}
				},
				 error: function (data, status, e) {
					console.log(data);
					$("#thumb").empty();
					alert("上传失败，请重新尝试。");
				}
			});
			} else {
				$("#thumb").empty();
				alert("请上传正确格式的图片！");
			}	
	 }
 
</script>
